package com.ictravi.iamunit.http;

import java.io.IOException;
import java.net.MalformedURLException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.ictravi.iamunit.IAMUnitTestCase;
import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.DefaultCredentialsProvider;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.ProxyConfig;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.WebResponse;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlImageInput;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlPasswordInput;
import com.gargoylesoftware.htmlunit.html.HtmlTextInput;

public class IAMUnit {
	private static Log LOG = LogFactory.getLog(IAMUnitTestCase.class);

	public static void getWeb()
	{
		LOG.info("...in http: getWeb()");
        WebClient wc = new WebClient(BrowserVersion.INTERNET_EXPLORER_8);

        wc.setJavaScriptEnabled(true);
        wc.setRedirectEnabled(true);
        wc.setThrowExceptionOnFailingStatusCode(true);
        wc.setThrowExceptionOnScriptError(true);
                
        DefaultCredentialsProvider scp = new DefaultCredentialsProvider();
        //scp.addProxyCredentials("sq22rq", "Dn-38416","10.76.64.34",8080);
        scp.addCredentials("tw20no","Novell10","10.76.64.34",8080,"Browse Infrastructure");
        scp.addCredentials("alouw@ictravi.com","kraz1y");
        wc.setCredentialsProvider(scp);
        
        ProxyConfig pConfig = new ProxyConfig();
        pConfig.setProxyHost("10.76.64.34");
        pConfig.setProxyPort(8080);
        //wc.setProxyConfig(pConfig);

        LOG.info("++++ before URL");
        try {
        	String site = "https://login.idfocus.nl/nidp/idff/sso?id=4&sid=0&option=credential&sid=0";
        	HtmlPage page = wc.getPage(site);
 
        	WebResponse rep = page.getWebResponse();
        	if (rep.getStatusCode() == 200)
        	{
        		LOG.info("Page Title: " + page.getTitleText());
        		//LOG.info("Page      : " + page.asXml());
        		
        		HtmlForm form = page.getFormByName("IDPLogin");
        		
        		HtmlTextInput tf1 = form.getInputByName("Ecom_User_ID");
        		HtmlPasswordInput tf2 = (HtmlPasswordInput)form.getInputByName("Ecom_Password");
        		tf1.setValueAttribute("alouw");
        		tf2.setValueAttribute("*dysan,m!");
        		HtmlImageInput tf3 = (HtmlImageInput)form.getInputByName("loginButton2");
        		// Get AM Login Result
        		HtmlPage page2 = (HtmlPage)tf3.click();
        		LOG.info("Page Title: " + page2.getTitleText());
        		//LOG.info(page2.asXml());
        		// Switch to Trac project page
        		HtmlPage page3 = wc.getPage("https://trac.idfocus.nl/projects/mdsmigratie/login");
        		LOG.info("Page Title: " + page3.getUrl() + " - " + page3.getTitleText());
        		//LOG.info(page3.asXml());
        	}

        	wc.closeAllWindows();
        } catch (MalformedURLException e)
        {
        	LOG.info("MAL URL Error: " + e.getMessage());
        }
        catch (FailingHttpStatusCodeException e)
        {
        	LOG.info("STATUS Error: " + e.getMessage());
        } catch (IOException e)
        {
        	LOG.info("IO Error: " + e.getMessage());
        } catch (Exception e)
        {
        	LOG.info("Exception Error: " + e.getMessage());
        }
	}

}
